//
// Description: 80. 删除有序数组中的重复项 II
// Created by Loading on 2025/2/22.
//

#include <bits/stdc++.h>

using namespace std;

int removeDuplicates(vector<int> &nums) {
    int n = nums.size();
    if (n <= 2) {
        return n;
    }

    // 快慢指针，慢指针指向即将赋值的位置，快指针遍历数组
    int slow = 2, fast = 2;
    while (fast < n) {
        // slow-2 位置 与 fast 位置比较，如果相等，说明已经达到两个
        if (nums[slow - 2] != nums[fast]) {
            nums[slow++] = nums[fast];
        }
        ++fast;
    }

    return slow;
}

int main() {
    vector<int> nums{0, 0, 1, 1, 1, 1, 2, 3, 3};

    auto res = removeDuplicates(nums);

    for (int i = 0; i < res; ++i) {
        cout << nums[i] << ' ';
    }
    cout << endl;

    return 0;
}